package com.bjpowernode.common.client;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HttpClient {
    public static String execHttpRequest(String requestUrl, String appcode, String name, String idCard) {
        String url = requestUrl;
        String appCode = appcode;

        String result = "";
        try {

            Map<String, String> params = new HashMap<>();
            params.put("idcard", idCard);
            params.put("name", name);


            OkHttpClient client = new OkHttpClient.Builder().build();
            FormBody.Builder formbuilder = new FormBody.Builder();
            Iterator<String> it = params.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                formbuilder.add(key, params.get(key));
            }
            FormBody body = formbuilder.build();
            Request request =
                    new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appCode).post(body).build();
            Response response = client.newCall(request).execute();


            System.out.println("返回状态码" + response.code() + ",message:" + response.message());
            result = response.body().string();
        } catch (Exception e) {
            e.printStackTrace(); //打印异常堆栈信息.

        }

        return result;
    }
}
